package cn.itsource.hrm.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.itsource.hrm.client.RedisClient;
import cn.itsource.hrm.controller.vo.SendSMSCodeVo;
import cn.itsource.hrm.util.SMSUtils;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.StrUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenyuqi
 * @version v1.0.0
 * @date 2020/6/2
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private RedisClient redisClient;

    /**
     * 生成图片验证码
     *
     * @return
     */
    @GetMapping("/getImageCode")
    public String getImageCode(@RequestParam("uuid") String uuid) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        //获取生成的验证码
        String code = lineCaptcha.getCode();
        //写入到redis
        String key = "CODE:IMAG." + uuid;
        redisClient.set(key, code);
        //响应验证码图片
        String imageBase64 = lineCaptcha.getImageBase64();
        return imageBase64;
    }

    /**
     * 发送短信验证码
     *
     * @param vo
     * @return
     */
    @PostMapping("/sendSMSCode")
    public AjaxResult sendSMSCode(@RequestBody SendSMSCodeVo vo){
        try {
            if (StringUtils.isEmpty(vo.getPhoneNum())) {
                return AjaxResult.me().setSuccess(false).setMessage("手机号码不能为空!");
            }
            if (StringUtils.isEmpty(vo.getCode())) {
                return AjaxResult.me().setSuccess(false).setMessage("请输入图形验证码!");
            }
            if (StringUtils.isEmpty(vo.getUuid())) {
                return AjaxResult.me().setSuccess(false).setMessage("前端标识不能为空!");
            }
            //判断图形验证码是否正确
            String redisKey = "CODE:IMAGE:" + vo.getUuid();
            String validateCode = redisClient.get(redisKey);
            //前端传来的和redis中的不相等
            if (!(vo.getCode().equals(validateCode))) {
                return AjaxResult.me().setSuccess(false).setMessage("图形验证码错误!");
            }

            //从redis中获取短信验证码
            redisKey = "CODE:SMS:" + vo.getPhoneNum();
            //value = {验证码},{超时时间}
            String redisValue = redisClient.get(redisKey);

            String code = null;
            Long lastSendTime = null;

            if (StringUtils.isNotEmpty(redisValue)) {
                String[] split = redisValue.split(",");
                code = split[0];
                lastSendTime = Long.valueOf(split[1]);

                //时间少于30为非法请求
                if (System.currentTimeMillis() - lastSendTime <= 30000) {

                    return AjaxResult.me().setSuccess(false).setMessage("非法请求!");
                }
            } else {
                //否则就是没发送过
                code = StrUtils.getRandomString(6);
            }

            lastSendTime = System.currentTimeMillis();
            //保存redis
            redisValue = code + "," + lastSendTime;
            //重发时间为20S，超时时间为5分钟
            redisClient.setex(redisKey, redisValue, 5 * 60);


            //发送短信
            SMSUtils.sendSMSCode(vo.getPhoneNum(), code);

            return AjaxResult.me().setSuccess(true).setMessage("发送成功!");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        }
    }





}
